<template>
  <div class="home">
    <div class="headers">
      <div class="headers-main">
        <Header></Header>
        <ly-tab
          v-model="selectedId"
          :items="items"
          :options="options"
          @change="changeTab"
        >
        </ly-tab>
      </div>
    </div>
    <section class="wrapper">
      <div>
        <div v-for="(item,i) in newData" :key="i">
          <Swiper 
            v-if="item.type=='swiperList'"
            :swiperList='item.data'
          ></Swiper>

          <Icons 
            v-if="item.type=='iconsList'"
            :iconsList='item.data'
          ></Icons>

          <Recommend 
            v-if="item.type=='recommendList'"
            :recommendList="item.data"
          ></Recommend>

          <Like
           v-if="item.type=='likeList'"
           :likeList="item.data"
           ></Like>

          <Ad 
            v-if="item.type=='adList'"
            :adList="item.data"
          ></Ad>
        </div>
      </div>
    </section>
    <Tabbar></Tabbar>
  </div>
</template>

<script>
import Tabbar from '@/components/common/Tabbar.vue';
import Header from '@/components/home/Header.vue';
import Swiper from "@/components/home/Swiper.vue";
import Icons from "@/components/home/Icons.vue"
import Recommend from '@/components/home/Recommend.vue'
import Like from '@/components/home/Like.vue'
import Ad from '@/components/home/Ad.vue'
// 滚动插件  必须添加在mounted
import BetterScroll from 'better-scroll'
import http from '@/common/api/request.js'

export default {
  components: { Tabbar, Header, Swiper, Icons, Recommend, Like, Ad },
  name: "HomeView",
  data() {
    return {
      selectedId: 0,
      items: [],          //头部数据
      newData:[],         //首页数据
      options: {
        activeColor: '#1d98bd'
      }
    }
  },
  methods: {
    // 头部栏点击事件
    changeTab(item, index) {
      this.addData(index)
    },
    // 点击头部发送请求
    async addData(index){
      let res=await http.$axios({
        url:'/api/index_list/'+index+'/data/1'
      })
      if(res.constructor!=Array){
        this.newData=res.data
      }else{
        this.newData=res
      }
      // 当dom执行完毕后执行betterScroll
      this.$nextTick(()=>{
        new BetterScroll('.wrapper', {
            movable: true,
            zoom: true,
            click:true,
        })
      })
    },

    // 异步请求数据
    async getData() {
      let res=await  http.$axios({
        url: '/api/index_list/0/data/1'
      })
      console.log(res)
      this.items=Object.freeze(res.topBar)
      this.newData=Object.freeze(res.data)

      // 当dom执行完毕后执行betterScroll
      this.$nextTick(()=>{
        new BetterScroll('.wrapper', {
            movable: true,
            zoom: true,
            click:true
        })
      })
    }
  },
  created() {
    this.getData()
  }
};
</script>

<style lang="scss" scoped>
.home {
  display: flex;
  flex-direction: column;
  width: 100vw;
  height: 100vh;
  overflow: hidden;
}
.headers {
  width: 100%;
  height: 2.48rem;
}
.headers-main {
  width: 100%;
  position: fixed;
  top: 0;
  left: 0;
}
section {
  flex: 1;
  overflow: hidden;
  // margin-top: 2.48rem;
}
::v-deep .ly-tabbar {
  box-shadow: none;
  border-bottom: none;
}
</style>
